package com.leanyuan.publi.dyno;

import com.leanyuan.publi.domain.House;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.jdbc.SQL;

public class HouseDynaSqlProvider {
    public String updateHouseSql(House house){
        return new SQL(){
            {
            UPDATE("house");
            if(house.getName()!=null){
                SET("name=#{name}");
            }
            if(house.getBuilding()!=null){
                SET("building_id=(select building_id from building where name=#{building.name})");
            }if(house.getHArea()!=null){
                SET("h_area=#{hArea}");
            }
            WHERE("house_id=#{houseId}");
        };
        }.toString();
    }

    public String insertHouseSql(House house){
        return new SQL(){
            {
                INSERT_INTO("house");
                VALUES("name","#{name}");
                VALUES("h_area","#{hArea}");
                VALUES("building_id","(SELECT building_id from building where name=#{building.name})");
            }
        }.toString();
    }

    public String update2HouseSql(House house){
        return new SQL(){
            {
                UPDATE("house");
                SET("user_id=null");
                SET("h_condition='未入住'");
                WHERE("house_id=#{houseId}");
            }
        }.toString();
    }
}
